package Avocado.visual {

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;

	public class ModalStage extends MovieClip {
		
		private var transp:Number;
		private var color:uint;
		private var zeroPoint:Point;
		private var lastPoint:Point;
		private var endPoint:Point;
		private var endLastPoint:Point;
		
		public function ModalStage(_transp:Number=0.6, _color:uint=0x000000) {
			super();
			this.transp = _transp;
			if (this.transp > 1){
				this.transp = 1;
			} else if (this.transp < 0){
				this.transp = 0;
			}
			this.color = _color;
			this.addEventListener(Event.ADDED_TO_STAGE, initThis);
		}
		
		private function initThis(e:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE, initThis);
			zeroPoint = globalToLocal(new Point(stage.x, stage.y));
			
			this.graphics.beginFill(0x000000, transp);
			this.graphics.drawRect(zeroPoint.x-stage.stageWidth, zeroPoint.y-stage.stageHeight, stage.stageWidth*4, stage.stageHeight*4);
			this.graphics.endFill();
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, removeThis);
			stage.addEventListener(Event.FULLSCREEN, updateThis);
			stage.addEventListener(Event.RESIZE, updateThis);
			updateThis();
		}
		
		private function updateThis(e:Event=null):void{
			// this.transform.concatenatedMatrix
			// this.transform.matrix = new Matrix(
			// trace(this.width, this.height, stage.stageWidth, stage.stageHeight);
			// trace(this.width / stage.stageWidth, this.height / stage.stageHeight);
			
			zeroPoint = globalToLocal(new Point(stage.x, stage.y));
			if (zeroPoint != lastPoint){
				this.graphics.clear();
				this.graphics.beginFill(color, transp);
				this.graphics.drawRect(zeroPoint.x-stage.stageWidth, zeroPoint.y-stage.stageHeight, stage.stageWidth*4, stage.stageHeight*4);
				this.graphics.endFill();
			}
			
			lastPoint = zeroPoint;
		}
		
		private function removeThis(e:Event):void{
			stage.removeEventListener(Event.RESIZE, updateThis);
			stage.removeEventListener(Event.FULLSCREEN, updateThis);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, updateThis);
		}
		
	}

}